package com.xiaomaoguai.curator;

import com.xiaomaoguai.curator.common.model.Model;
import com.xiaomaoguai.curator.common.anno.Provider;
import com.xiaomaoguai.curator.common.conf.ServerConfig;
import com.xiaomaoguai.curator.common.zookeeper.server.ServerSideZkThread;
import org.aeonbits.owner.ConfigFactory;
import org.reflections.Reflections;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @fileName: ServerMain.java
 * @author: WeiHui.Zhang
 * @date: 2016-11-04  18:28
 * @version: v1.0.0
 */
public class ServerMain {

	public static void main(String[] args) throws Exception {
		ServerConfig config = ConfigFactory.create(ServerConfig.class);

		Reflections reflections = new Reflections(Arrays.asList(config.scanPackage()));
		Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(Provider.class);
		Set<Model> modelSet = new HashSet<>(classSet.size());

		for (Class<?> clazz : classSet) {
			Provider provider = clazz.getAnnotationsByType(Provider.class)[0];

			String group = provider.group();
			String edition = provider.edition();
			String name = clazz.getName();

			Model model = new Model(group, name, edition);
			modelSet.add(model);
		}
		System.out.println(modelSet);

		Thread zkThread = new Thread(new ServerSideZkThread(modelSet, config));
		zkThread.setName("zookeeper注册服务");
		zkThread.start();

		System.in.read();
	}

}
